<?php
/**
 * Created by PhpStorm.
 * User: RenBin
 * Date: 2015/12/30
 * Time: 11:11
 */

namespace common\models\Service;


use common\models\CommonFunction;
use common\models\DAO\DayAccrual;
use common\models\DAO\User;
use common\models\DAO\UserCard;
use common\models\DAO\UserTransfer;
use common\models\DAO\UserWalletLog;
use common\msg\UserMsg;

class TransferService {

    public static function user($param){
        $return = ['status'=>0, 'msg'=>'该用户信息不存在！'];
        $userInfo = $param['user'];
        $userID = $userInfo['userID'];
        $pwd = 0;
        $userInfo['dealPassword'] && $pwd = 1;
        if(!empty($param['user_key'])){
            $user_key = CommonFunction::decrypt(trim($param['user_key']));
            $data = explode('+', $user_key);
            if(!isset($data[1]) || (time() - intval($data[1])) > 120){
                $return['msg'] = '二维码已失效。';
                return $return;
            }
            $user = User::findOne(['appKey'=>$data[0]]);
            if($user){
                if($user['id'] == $userID){
                    $return['msg'] = '不能给自己的账号转账';
                    return $return;
                }
                $remainder = intval(Wallet::remainder($userID)) / 100;
                $return = ['status'=>1, 'tel'=>CommonFunction::decrypt($user['phone']),'remainder'=>$remainder,
                    'pwd'=>$pwd, 'name'=>$user['realName'], 'head'=>$user['avatar']];
            }

        }else{
            $user = User::findOne(['phone'=>CommonFunction::encrypt($param['tel'])]);
            if($user){
                if($user['id'] == $userID){
                    $return['msg'] = '不能给自己的账号转账';
                    return $return;
                }
                $remainder = intval(Wallet::remainder($userID)) / 100;
                $return = ['status'=>1, 'tel'=>CommonFunction::decrypt($user['phone']),'remainder'=>$remainder,
                    'pwd'=>$pwd, 'name'=>$user['realName'], 'head'=>$user['avatar']];
            }
        }
        return $return;
    }

    public static function auth($param){
        $card = $param['card_no'];
        $no_id = $param['id_no'];
        $name = $param['acct_name'];
        $user = $param['user'];
        $userID = $user['userID'];
        $return = ['status'=>0, 'msg'=>'输入信息有误！请重新输入。'];
        $cardInfo = UserCard::findOne(['userID'=>$userID]);
        if(!$cardInfo)
            return ['status'=>0, 'msg'=>'您的银行卡尚未激活，是否激活？','card'=>'0'];

        if($card != CommonFunction::decrypt($cardInfo['card']) || $no_id != $user['id_no'] || $name != $user['name'])
            return $return;
        $userModel = User::findOne($userID);
        $userModel->dealPassword = md5(CommonFunction::encrypt(trim($param['password'])));
        $result = $userModel->save();
        if(!$result)
            return ['status'=>0 ,'msg'=>UserMsg::$time_out];
        return ['status'=>1];
    }

    public static function getList($userID, $page = 1, $limit = 10){
        $start = (intval($page) - 1) * $limit;
        $result = UserWalletLog::find()->where(['userID'=>$userID])->andWhere('(type=2 and road=6) or (type=1 and road=17)')->asArray()->limit($limit)->offset($start)->orderBy('id desc')->all();
        $list = [];
        foreach($result as $k=>$v){
            $list[$k]['type'] = $v['type'];
            $list[$k]['money'] = CommonFunction::formatMoney($v['money']);
            $list[$k]['desc'] = $v['desc'];
            $list[$k]['time'] = date('Y-m-d H:i',$v['createTime']);
        }
        return $list;
    }

    public static function deal($param){
        $return = ['status'=>0, 'msg'=>UserMsg::$time_out];
        $user = $param['user'];
        $userID = $user['userID'];
        $deal_pwd = $user['dealPassword'];
        $tel = $param['tel'];

        $password = md5(CommonFunction::encrypt(trim($param['password'])));
        $money = floatval($param['money_order']) * 100;

        if($deal_pwd != $password){
            $return['msg'] = UserMsg::$deal_password_error;
            return $return;
        }
        $remainder = Wallet::remainder($userID);
        if($money > $remainder){
            $return['msg'] = UserMsg::$no_money;
            return $return;
        }
        $transfer_user = User::findOne(['phone'=>CommonFunction::encrypt($tel)]);
        if(!$transfer_user){
            $return['msg'] = UserMsg::$transfer_error;
            return $return;
        }
        $transaction = \Yii::$app->db->beginTransaction();
        $transferModel = new UserTransfer();
        $today = time();
        $transferModel->userID = $userID;
        $transferModel->transferUserID = $transfer_user['id'];
        $transferModel->money = $money;
        $transferModel->createTime = $today;
        $transfer_result = $transferModel->save();
        if(!$transfer_result){
            $transaction->rollBack();
            return $return;
        }
        $transferID = $transferModel->id;
        $deduct_param['id'] = $transferID;
        $deduct_param['userID'] = $userID;
        $deduct_param['transferUserID'] = $transfer_user['id'];
        $deduct_param['money'] = $money;
        $deduct_param['desc'] = '转账给好友'.$tel;
        $deduct_param['createTime'] = $today;
        $deduct_param['wallet_type'] = 2;
        $deduct_param['wallet_road'] = 6;
        $deduct_result = Wallet::transfer_deduct($deduct_param);
        if(!$deduct_result){
            $transaction->rollBack();
            return $return;
        }
        $date = date('Ymd');
        //更新用户账号余额
        $dayAccrual = new DayAccrual();
        $dayAccrual->userID = $transfer_user['id'];
        $dayAccrual->date = $date;
        $dayAccrual->money = $money;
        $dayAccrual->invest = $money;
        $dayAccrual->createTime = $today;
        $dayAccrual->updateTime = $today;
        $dayAccrual->gmID = $transferID;
        $dayAccrual->typeID = 17;
        $accrual_result  = $dayAccrual->save();
        if(!$accrual_result){
            $transaction->rollBack();
            return $return;
        }
        //钱包明细
        $user_wallet = new UserWalletLog();
        $user_wallet->type = 1;
        $user_wallet->money = $money;
        $user_wallet->createTime = $today;
        $user_wallet->userID = $transfer_user['id'];
        $user_wallet->road = 17;
        $user_wallet->desc = '好友'.$user['tel'].'转账';
        $user_wallet->gmID = $transferID;
        $user_wallet_result = $user_wallet->save();
        if(!$user_wallet_result){
            $transaction->rollBack();
            return $return;
        }
        $transaction->commit();
        return ['status'=>1, 'no'=>$transferID];


    }

    public static function info($userID, $id){
        $info = UserTransfer::findOne($id);
        $return = ['status'=>0, 'msg'=>UserMsg::$time_out];
        if(!$info || $info['userID'] != $userID)
            return $return;
        $user = User::findOne($info['transferUserID']);
        if(!$user)
            return $return;
        $name = '';
        $user['realName'] && $name = $user['realName'];
        $tel = CommonFunction::decrypt($user['phone']);
        return ['status'=>1, 'tel'=>$tel, 'name'=>$name,'date'=>date('Y-m-d H:i',$info['createTime']), 'money'=>CommonFunction::formatMoney($info['money'])];
    }

}